package com.ruoyi.module.service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.module.domain.BookExtInfo;
import com.ruoyi.module.domain.BookExtInfoExample;
import com.ruoyi.module.domain.BookInfo;
import com.ruoyi.module.domain.BookInfoExample;
import com.ruoyi.module.mapper.BookExtInfoMapper;
import com.ruoyi.module.mapper.BookInfoMapper;
import lombok.RequiredArgsConstructor;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 图书西信息Service业务层处理
 *
 * @author ruoyi
 * @date 2022-04-13
 */
@DS("bookmgr")
@Service
@RequiredArgsConstructor
public class CmBookInfoService {
    private final BookInfoMapper bookInfoMapper;
    private final BookExtInfoMapper bookExtInfoMapper;

    /**
     * 查询图书西信息
     *
     * @param id 图书西信息主键
     * @return 图书西信息
     */
    public BookInfo selectCmBookInfoById(Long id) {
        return bookInfoMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询图书西信息列表
     *
     * @return 图书西信息
     */
    public List<BookInfo> selectCmBookInfoList(String spbs, String tm, String sm, String zyz) {
        BookInfoExample example = new BookInfoExample();
        BookInfoExample.Criteria or = example.or();

        if (StringUtils.isNotEmpty(spbs)) {
            or.andSpbsEqualTo(spbs);
        }
        if (StringUtils.isNotEmpty(tm)) {
            or.andTmEqualTo(tm);
        }
        if (StringUtils.isNotEmpty(sm)) {
            or.andSmLike(sm);
        }
        if (StringUtils.isNotEmpty(zyz)) {
            or.andZyzLike(zyz);
        }

        return bookInfoMapper.selectByExample(example);
    }

    /**
     * 新增图书西信息
     *
     * @param cmBookInfo 图书西信息
     * @return 结果
     */
    public int insertCmBookInfo(BookInfo cmBookInfo) {
        return bookInfoMapper.insertSelective(cmBookInfo);
    }

    /**
     * 修改图书西信息
     *
     * @param cmBookInfo 图书西信息
     * @return 结果
     */
    public int updateCmBookInfo(BookInfo cmBookInfo) {
        return bookInfoMapper.updateByPrimaryKeySelective(cmBookInfo);
    }

    /**
     * 批量删除图书西信息
     *
     * @param ids 需要删除的图书西信息主键
     * @return 结果
     */
    public int deleteCmBookInfoByIds(List<Long> ids) {
        BookInfoExample example = new BookInfoExample();
        example.or().andIdIn(ids);

        return bookInfoMapper.deleteByExample(example);
    }

    /**
     * 删除图书西信息信息
     *
     * @param id 图书西信息主键
     * @return 结果
     */
    public int deleteCmBookInfoById(Long id) {
        return bookInfoMapper.deleteByPrimaryKey(id);
    }

    public int clearBySpbs(List<String> spbses) {
        if (!CollectionUtils.isEmpty(spbses)) {
            BookInfoExample example = new BookInfoExample();
            BookInfoExample.Criteria criteria = example.or();
            criteria.andSpbsIn(spbses);

            return bookInfoMapper.deleteByExample(example);
        } else {
            return 1;
        }
    }

    public List<BookInfo> findBySpbs(List<String> spbses) {
        BookInfoExample example = new BookInfoExample();
        BookInfoExample.Criteria criteria = example.or();
        criteria.andSpbsIn(spbses);

        return bookInfoMapper.selectByExample(example);
    }

    public int batchUpdateBookInfo(List<BookInfo> infos) {
        List<String> spbses = infos.stream().map(BookInfo::getSpbs).collect(Collectors.toList());

        List<BookInfo> bookInfos = this.findBySpbs(spbses);
        Map<String, BigDecimal> bookInfoStoreMap = bookInfos.stream().collect(Collectors.toMap(BookInfo::getSpbs, BookInfo::getZjs, (v1, v2) -> v2));

        List<BookInfo> needUpdateList = infos.stream().filter(v -> {
            if (!bookInfoStoreMap.containsKey(v.getSpbs())) {
                return Boolean.TRUE;
            }

            if (!Objects.equals(bookInfoStoreMap.get(v.getSpbs()), v.getZjs())) {
                return Boolean.TRUE;
            }
            return Boolean.FALSE;
        }).collect(Collectors.toList());

        spbses = needUpdateList.stream().map(BookInfo::getSpbs).collect(Collectors.toList());
        this.clearBySpbs(spbses);

        return this.batchInsert(needUpdateList);
    }


    public int batchInsert(List<BookInfo> bookInfos) {
        if (!CollectionUtils.isEmpty(bookInfos)) {

            // 处理insert图片问题
            List<String> collect = bookInfos.stream().map(BookInfo::getTm).distinct().collect(Collectors.toList());

            // 查询图书对应的图片和详情
            Map<String, BookExtInfo> bookExtInfoMap = new HashMap<>();
            if (!CollectionUtils.isEmpty(collect)) {
                BookExtInfoExample example2 = new BookExtInfoExample();
                example2.or().andIsbnIn(collect);
                List<BookExtInfo> bookExtInfos = bookExtInfoMapper.selectByExampleWithBLOBs(example2);
                bookExtInfoMap = Optional.ofNullable(bookExtInfos).orElse(Lists.newArrayList()).stream().collect(Collectors.toMap(BookExtInfo::getIsbn, v -> v, (v1, v2) -> v1));
            }

            List<BookInfo> insertList = Lists.newArrayList();
            for (BookInfo v : bookInfos) {
                if (bookExtInfoMap.containsKey(v.getTm())) {
                    BookExtInfo bookExtInfo = bookExtInfoMap.get(v.getTm());
                    v.setImagePath(bookExtInfo.getImagePath());
                    v.setRemarks(bookExtInfo.getRemarks());
                }

                insertList.add(v);
            }

            // 批量插入
            if (!CollectionUtils.isEmpty(insertList)) {
                return bookInfoMapper.batchInsert(insertList);
            }
        }
        return 0;
    }

    public List<BookInfo> queryByTms(List<String> tms) {
        BookInfoExample example = new BookInfoExample();
        example.or()
                .andStordIdEqualTo("100029")
                .andTmIn(tms);

        return bookInfoMapper.selectByExample(example);
    }

    public Long countByTime(long ctime) {
        BookInfoExample example = new BookInfoExample();
        example.or()
                .andStordIdEqualTo("100029")
                .andCtimeGreaterThan(ctime);

        return bookInfoMapper.countByExample(example);
    }

    public List<BookInfo> queryBySpbses(List<String> spbses) {
        BookInfoExample example = new BookInfoExample();
        example.or()
                .andStordIdEqualTo("100029")
                .andSpbsIn(spbses);

        return bookInfoMapper.selectByExample(example);
    }
}
